package cn.baddonkey.superqueryjavasdk.json;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Type;

public class GsonParser implements JsonParser {
    @Setter
    @Getter
    private Gson gson;

    public GsonParser() {
        this.gson = new Gson();
    }

    @Override
    public <T> T from(String json, Class<T> type) {
        return this.gson.fromJson(json, type);
    }

    @Override
    public <T> T from(String json, Type type) {
        return this.gson.fromJson(json, this.getType(type));
    }

    @Override
    public String to(Object o) {
        return this.gson.toJson(o);
    }

    @Override
    public Type getType(Type type) {
        return TypeToken.get(type).getType();
    }
}
